<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="住址" prop="address">
        <owneraddress @getAddressid="showAddress" ></owneraddress>
      </el-form-item>
      <el-form-item label="房屋类型编号" prop="estId">
        <el-input v-model="dataForm.estId" placeholder="房屋类型编号"></el-input>
      </el-form-item>
      <el-form-item label="是否在用" prop="isused">
        <el-input v-model="dataForm.isused" placeholder="是否在用"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import owneraddress from "@/views/common/owneraddress.vue";
export default {
  components: {
    owneraddress: owneraddress
  },
  data() {
    return {
      visible: false,
      dataForm: {
        blocId: 0,
        uniId: "",
        roomId: "",
        estId: "",
        isused: ""
      },
      dataRule: {
        uniId: [{ required: true, message: "单元号不能为空", trigger: "blur" }],
        roomId: [
          { required: true, message: "门牌号不能为空", trigger: "blur" }
        ],
        estId: [
          { required: true, message: "房屋类型编号不能为空", trigger: "blur" }
        ],
        isused: [
          { required: true, message: "是否在用不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 设置地址
    showAddress(data) {
      this.dataForm.blocId = data[0];
      this.dataForm.uniId = data[1];
      this.dataForm.roomId = data[2];
      console.log("添加业主",this.dataForm)
    },
    init(id) {
      this.dataForm.blocId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.blocId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/estates/info/${this.dataForm.blocId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.uniId = data.estates.uniId;
              this.dataForm.roomId = data.estates.roomId;
              this.dataForm.estId = data.estates.estId;
              this.dataForm.isused = data.estates.isused;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/estates/${!this.dataForm.blocId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              blocId: this.dataForm.blocId || undefined,
              uniId: this.dataForm.uniId,
              roomId: this.dataForm.roomId,
              estId: this.dataForm.estId,
              isused: this.dataForm.isused
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
